#! /bin/bash

# ----------------------------------------------------------------------------
#
# Install chia on a fresh system
#
# yqm_leaf@163.com
#
# 05/06/21
#
# ----------------------------------------------------------------------------

set -e

[ $UID -ne 0 ] && { echo "Need to be root." ; exit 1 ; }

centos_install () {
    yum install epel-release -y
    yum update -y
    # Compiling python 3.7 is generally required on CentOS 7.7 and newer
    yum install gcc openssl-devel bzip2-devel zlib-devel libffi libffi-devel -y
    yum install libsqlite3x-devel -y
    # possible that on some RHEL based you also need to install
    yum groupinstall "Development Tools" -y
    yum install python3-devel gmp-devel  boost-devel libsodium-devel -y
    
    yum install wget -y
    wget https://www.python.org/ftp/python/3.7.7/Python-3.7.7.tgz
    tar -zxvf Python-3.7.7.tgz ; cd Python-3.7.7
    ./configure --enable-optimizations; sudo make -j$(nproc) altinstall; cd ..
    # Download and install the source version
    git clone https://github.com/Chia-Network/chia-blockchain.git -b latest
    cd chia-blockchain
    bash install.sh
    . ./activate
}


ubuntu_install () {
    apt-get update
    apt-get upgrade -y

    # Install Git
    apt install git -y

    # Checkout the source and install
    git clone https://github.com/Chia-Network/chia-blockchain.git -b latest --recurse-submodules
    cd chia-blockchain
    bash install.sh
    . ./activate
}

if [ -f "/etc/centos-release" ] ; then
    centos_install
elif grep -q "DISTRIB_ID=Ubuntu" /etc/lsb-release 2>/dev/null; then
    ubuntu_install
else
    echo "OS not supported."
    exit 1
fi
